package leetcode100;

// TODO 滑动窗口 [经典] 【-】 无重复字符的最长子串
// TODO https://leetcode.cn/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-by-bananajin-6-fxvb/

import java.util.HashMap;
import java.util.Map;

public class lc100_0003 {

    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), l = 0, r = 0;
        Map<Character, Integer> m = new HashMap<>();
        int maxLen = 0;

        while (r < n) {
            if (m.containsKey(s.charAt(r))) {
                // map 包含当前字符
                m.remove(s.charAt(l));
                l++;
            } else {
                // map 不包含当前字符
                maxLen = Math.max(maxLen, r-l+1);
                m.put(s.charAt(r), 1);
                r++;
            }
        }

        return maxLen;
    }

}
